<?php defined('SYSPATH') OR die('No direct access allowed.');

require_once("alipayescow/class/alipay_service.php");

class Pay_Controller extends Controller 
{
	private $product;
	private $order;

	public function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$authentic = new Auth;
		if (!$authentic->logged_in())
		{
			url::redirect('');
		}

		if ($product_id = $this->input->post('product_id'))
		{
			$this->product = ORM::factory('product');	
			$this->product->get_product($product_id);
			if ($this->product->loaded AND $this->create_order($authentic->get_user(), $this->product))
			{
				$this->alipay($this->order);
				return;
			}
		}

		url::redirect();
	}

	private function create_order($user, $product)
	{
		$this->order = ORM::factory('order');
		$this->order->user_id = $user->id;
		$this->order->product_id = $product->id;
		if ($this->order->save())
		{
			return true;
		}
		return false;
	}

	private function alipay($order)
	{
		require "alipayescow/alipayto.php";
	}
	
	function alipayto(){
		/*
		require "alipayescow/alipayto.php";
		 */
	}
	
	function return_url()
	{
		require "alipayescow/return_url.php";
	}
	
	function notify_url()
	{
		require "alipayescow/notify_url.php";
	}
}
